package com.px.user.mapper;


import com.px.user.entity.Withdraw;
import com.px.user.model.WithdrawSum;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface WithdrawMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into withdraw (userId,name,bankCard,bankName,money,status) values " +
            "(#{userId},#{name},#{bankCard},#{bankName},#{money},#{status})")
    void insert(Withdraw withdraw);

    @Select("select ifnull(sum(money),0) as total, ifnull(count(money),0) as count from withdraw " +
            "where userId=#{userId} and status in ('AUDITING', 'SUCCESS') and " +
            "createTime between #{startTime} and #{endTime}")
    @Results({
            @Result(property = "total", column = "total"),
            @Result(property = "count", column = "count")
    })
    WithdrawSum sumByUserId(Withdraw withdraw);

    @Select({"<script>",
            "select withdraw.*,user.nickname from withdraw left join user on withdraw.userId=user.id",
            "<where>",
            " <if test='userId != null'>and withdraw.userId = #{userId}</if>",
            " <if test='startTime != null'>and withdraw.createTime >= #{startTime}</if>",
            " <if test='endTime != null'>and withdraw.createTime <![CDATA[<=]]> #{endTime}</if>",
            " <if test='status != null'>and withdraw.status = #{status}</if>",
            "</where>",
            "order by ${orderField} desc limit #{startIndex},#{limit}",
            "</script>"})
    @Results({@Result(property = "nickname", column = "nickname")})
    List<Withdraw> paging(Withdraw withdraw);

    @Select({"<script>",
            "select count(1) from withdraw",
            "<where>",
            "<if test='userId != null'>and userId = #{userId}</if>",
            "<if test='startTime != null'>and createTime >= #{startTime}</if>",
            "<if test='endTime != null'>and createTime <![CDATA[<=]]> #{endTime}</if>",
            "<if test='status != null'>and status = #{status}</if>",
            "</where>",
            "</script>"})
    Integer pagingCount(Withdraw withdraw);

    @Update({"<script>",
            "update withdraw",
            " <set>",
            "  <if test='name != null'>name=#{name},</if>",
            "  <if test='bankCard != null'>bankCard=#{bankCard},</if>",
            "  <if test='bankName != null'>bankName=#{bankName},</if>",
            "  <if test='status != null'>status=#{status}</if>",
            " </set>",
            "where id=#{id}",
            "</script>"})
    void update(Withdraw withdraw);

    @Select("select * from withdraw where id=#{id}")
    Withdraw findById(Long id);
}
